package github.kawaiior.yuanchuxiuzhen.common.tileentity;

import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.common.container.GongFaCreateContainer;
import github.kawaiior.yuanchuxiuzhen.common.container.ManaStoneConverterContainer;
import github.kawaiior.yuanchuxiuzhen.core.init.ItemInit;
import github.kawaiior.yuanchuxiuzhen.core.init.TileEntityInit;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;

public class ManaStoneConverterTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {
    private int tickTime = 0;
    private Inventory manaInventory = new Inventory(1);
    private Inventory itemInventory = new Inventory(1);

    public ManaStoneConverterTileEntity() {
        this(TileEntityInit.MANA_STONE_CONVERTER_TILE_ENTITY.get());
    }

    public ManaStoneConverterTileEntity(TileEntityType<?> tileEntityTypeIn) {
        super(tileEntityTypeIn);
    }

    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("container."+ Yuanchuxiuzhen.MODID+".example_gui_block");
    }

    @Nullable
    @Override
    public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity player) {
        return new ManaStoneConverterContainer(id, playerInventory, this);
    }

    public static Map<Item, ItemStack> manaStoneConverterMap = new HashMap<>();

    static {
        manaStoneConverterMap.put(Items.DIAMOND.getItem(), new ItemStack(ItemInit.MANA_STONE_ITEM_LEVEL_5.get()));
        manaStoneConverterMap.put(Items.EMERALD.getItem(), new ItemStack(ItemInit.MANA_STONE_ITEM_LEVEL_5.get()));
        manaStoneConverterMap.put(Items.GOLD_INGOT.getItem(), new ItemStack(ItemInit.MANA_STONE_ITEM_LEVEL_4.get()));
        manaStoneConverterMap.put(Items.IRON_INGOT.getItem(), new ItemStack(ItemInit.MANA_STONE_ITEM_LEVEL_3.get()));
    }

    @Override
    public void tick() {
        tickTime++;
        if (tickTime<20){
            return;
        }
        tickTime = 0;

        ItemStack itemStack = this.itemInventory.getStackInSlot(0);
        Item item = itemStack.getItem();
        if (manaStoneConverterMap.containsKey(item)){
            this.manaInventory.setInventorySlotContents(0, manaStoneConverterMap.get(item).copy());
        }else {
            this.manaInventory.setInventorySlotContents(0, ItemStack.EMPTY);
        }
    }

    @Override
    public void read(BlockState state, CompoundNBT nbt) {
        this.manaInventory.setInventorySlotContents(0, ItemStack.read(nbt.getCompound("mana_item")));
        this.itemInventory.setInventorySlotContents(0, ItemStack.read(nbt.getCompound("item")));
        super.read(state, nbt);
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {
        compound.put("mana_item", this.manaInventory.getStackInSlot(0).copy().serializeNBT());
        compound.put("item", this.itemInventory.getStackInSlot(0).copy().serializeNBT());
        return super.write(compound);
    }

    public Inventory getManaInventory() {
        return manaInventory;
    }

    public Inventory getItemInventory() {
        return itemInventory;
    }
}
